package com.wb.oldwomen;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.MotionEvent;

import com.wb.fw.GateActivity;
import com.wb.fw.Global;
import com.wb.fw.IConstants;
import com.wb.fw.SoundManager;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.ColorLayer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor4B;

public class UIIGM extends ColorLayer implements IConstants {
	public static UIIGM make(GameScene parent) {
		return new UIIGM(parent, WYColor4B.make(255, 255, 255, 127));
	}
	GameScene mparent;
	AlertDialog mdialog;
	Button mbtnSOn,mbtnSOff;
	protected UIIGM(GameScene parent, WYColor4B color) {
		super(color);
		mparent = parent;
		setKeyEnabled(true);
		setTouchEnabled(true);
		
		Sprite bg = Sprite.make(R.drawable.igm_panel);
		bg.autoRelease();
		bg.setScale(Global.sScaleRate);
		bg.setPosition(Global.sWYSize.width/2, Global.sWYSize.height/2);
		addChild(bg);
		
		float panel_x = Global.sWYSize.width/2 - bg.getWidth()/2*Global.sScaleRate;
		float panel_y = Global.sWYSize.height/2 + bg.getHeight()/2*Global.sScaleRate;
		Button btnmap = Button.make(R.drawable.igm_map_n, R.drawable.igm_map_p, this, "xlExitLevel");
		btnmap.autoRelease();
		btnmap.setScale(Global.sScaleRate);
		btnmap.setAnchor(0, 1);
		btnmap.setPosition(panel_x + 55*Global.sScaleRate, panel_y - 40*Global.sScaleRate);
		addChild(btnmap);
		
		Button btncontinue = Button.make(R.drawable.igm_continue_n, R.drawable.igm_continue_p, this, "xlContinue");
		btncontinue.autoRelease();
		btncontinue.setScale(Global.sScaleRate);
		btncontinue.setAnchor(0, 1);
		btncontinue.setPosition(panel_x + 33*Global.sScaleRate, panel_y - 143*Global.sScaleRate);
		addChild(btncontinue);
		
		Button btnclose = Button.make(R.drawable.igm_close_n, R.drawable.igm_close_p, this, "xlClose");
		btnclose.autoRelease();
		btnclose.setScale(Global.sScaleRate);
		btnclose.setAnchor(0, 1);
		btnclose.setPosition(panel_x + 282*Global.sScaleRate, panel_y - 13*Global.sScaleRate);
		addChild(btnclose);
		
		float offset = 10*Global.sScaleRate;
		mbtnSOn = Button.make(R.drawable.soundon_n, R.drawable.soundon_p, R.drawable.soundon_n, R.drawable.soundon_n, this, "xlCloseSound");
		mbtnSOn.autoRelease();
		mbtnSOn.setAnchor(0, 1);
		mbtnSOn.setPosition(Global.sStartX+offset, Global.sStartY-offset);
		mbtnSOn.setScale(Global.sScaleRate);
		mbtnSOn.setVisible(false);
		addChild(mbtnSOn);
		
		mbtnSOff = Button.make(R.drawable.soundoff_n, R.drawable.soundoff_p, R.drawable.soundoff_n, R.drawable.soundoff_n, this, "xlOpenSound");
		mbtnSOff.autoRelease();
		mbtnSOff.setAnchor(0, 1);
		mbtnSOff.setPosition(Global.sStartX+offset, Global.sStartY-offset);
		mbtnSOff.setScale(Global.sScaleRate);
		mbtnSOff.setVisible(false);
		addChild(mbtnSOff);
		
		checkSoundState();
		
		Button btnhelp = Button.make(R.drawable.help_n, R.drawable.help_p, R.drawable.help_n, R.drawable.help_n, this, "xlHelp");
		btnhelp.autoRelease();
		btnhelp.setAnchor(1, 1);
		btnhelp.setPosition(Global.sEndX-offset, Global.sStartY-offset);
		btnhelp.setScale(Global.sScaleRate);
		addChild(btnhelp);
	}
	public void tick() {
		
	}
	
////////////////上面是模板/////////////////////////////
	
	public void xlContinue() {
		xlbackGame();
	}
	public void xlClose() {
		xlbackGame();
	}
	
	public final void xlbackGame() {
		SoundManager.clickMenuItem();
		mparent.switchState(GS_GAME_RUNNING);
	}
	public void xlHelp() {
		SoundManager.clickMenuItem();
		mparent.switchState(GS_GAME_HELP);
	}
	/**
	 * 关闭声音
	 */
	public void xlCloseSound() {
		SoundManager.clickMenuItem();
		mbtnSOn.setVisible(false);
		mbtnSOff.setVisible(true);
		SoundManager.enable(false);
	}

	/**
	 * 打开声音
	 */
	public void xlOpenSound() {
		mbtnSOn.setVisible(true);
		mbtnSOff.setVisible(false);
		SoundManager.enable(true);
		SoundManager.resumeMusic();
	}
	
	/**
	 * 进入主菜单的时候，需要确定声音开关ui。
	 */
	public void checkSoundState() {
		mbtnSOn.setVisible(false);
		mbtnSOff.setVisible(false);
		(SoundManager.isSoundOpen()?mbtnSOn:mbtnSOff).setVisible(true);
	}
	
	public void xlExitLevel() {
		SoundManager.clickMenuItem();
		if (mdialog == null || !mdialog.isShowing()) {
			GateActivity.sInstance.runOnUiThread(new Runnable() {

				@Override
				public void run() {
					if (mdialog == null) {
						mdialog = new AlertDialog.Builder(GateActivity.sInstance)
						.setMessage("是否退出？")
						.setNegativeButton("否", null)
						.setPositiveButton("是", new OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								mparent.switchState(GS_GAME_LEVEL_SELECT);
							}
						}).create();
					}
					mdialog.show();
				}
			});
		}
	}

	@Override
	protected boolean onBackButton() {
		if (isVisible()) {
			xlbackGame();
		}
		return true;
	}
	
	@Override
	public boolean wyTouchesBegan(MotionEvent event) {
		return true;
	}
	
	
}
